#include "netcfghelper.h"
#include "netcfgadaptor.h"
#include "../netcfgactions.h"

#include <QDBusConnection>
#include <QCoreApplication>

#include <KProcess>

#include <PolicyKit/polkit-qt/Auth>


const char *netcfgBin = "/usr/bin/netcfg";
const char *netcfgAutoWirelessBin = "/usr/bin/netcfg-auto-wireless";
const char *path = "bin:/usr/bin:/sbin:/usr/sbin:/usr/bin/perlbin/site:/usr/bin/perlbin/vendor:/usr/bin/perlbin/core";


class NetcfgHelperPrivate
{
    public:

        NetcfgHelper *q;

        NetcfgHelperPrivate(NetcfgHelper *netcfgHelper) :
            q(netcfgHelper)
        {
        }

        bool isAuthorized(const QString &actionId)
        {
            PolkitQt::Auth::Result result;
            result = PolkitQt::Auth::isCallerAuthorized(actionId, q->message().service(), true);
            if (result != PolkitQt::Auth::Yes) {
                qDebug() << "Not authorized";
                QCoreApplication::instance()->quit();
                return false;
            }
            return true;
        }
};


NetcfgHelper::NetcfgHelper(QObject *parent) :
    QObject(parent),
    d(new NetcfgHelperPrivate(this))
{
    new NetcfgAdaptor(this);

    QDBusConnection bus = QDBusConnection::systemBus();

    // it fails on DBus activation, reason is unclear, just go on
    bus.registerService("org.archlinux.netcfg");

    if (!bus.registerObject("/", this)) {
        qDebug() << "Unable to register service interface to dbus";
        QCoreApplication::instance()->quit();
        return;
    }
}

void NetcfgHelper::up(const QString &profile)
{
    if (!d->isAuthorized(NetcfgActionId::Up)) {
        return;
    }
    KProcess process;
    process.setOutputChannelMode(KProcess::SeparateChannels);
    process.setEnv("PATH", path);
    QStringList args(profile);
    process.setProgram(netcfgBin, args);
    process.execute();
    QCoreApplication::instance()->quit();
}

void NetcfgHelper::down(const QString &profile)
{
    if (!d->isAuthorized(NetcfgActionId::Down)) {
        return;
    }
    KProcess process;
    process.setOutputChannelMode(KProcess::SeparateChannels);
    process.setEnv("PATH", path);
    QStringList args;
    args << "down" << profile;
    process.setProgram(netcfgBin, args); 
    process.execute();
    QCoreApplication::instance()->quit();
}

void NetcfgHelper::allDown()
{
    if (!d->isAuthorized(NetcfgActionId::AllDown)) {
        return; 
    }
    KProcess process;
    process.setOutputChannelMode(KProcess::SeparateChannels);
    process.setEnv("PATH", path);
    QStringList args("all-down");
    process.setProgram(netcfgBin, args);
    process.execute();
    QCoreApplication::instance()->quit();
}

void NetcfgHelper::autoWireless()
{
    if (!d->isAuthorized(NetcfgActionId::AutoWireless)) {
        return;
    }
    KProcess process;
    process.setOutputChannelMode(KProcess::SeparateChannels);
    process.setEnv("PATH", path);
    QStringList args("wlan0");
    process.setProgram(netcfgAutoWirelessBin, args);
    process.execute();
    QCoreApplication::instance()->quit();
}


#include "netcfghelper.moc"


int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    new NetcfgHelper(0);
    return app.exec();
}
